package com.learn.semaphore;

import org.apache.log4j.Logger;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/30
 */
public class TestSemaphore {
    final static Logger log=Logger.getLogger(TestSemaphore.class);

    public static void main(String[] args) {
//        1.创建semaphore对象
        Semaphore semaphore=new Semaphore(3);

//        2.10个线程同时运行
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    log.info("running.....");
                    TimeUnit.SECONDS.sleep(1);
                    log.info("end....");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
